package com.dianrui.agent.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.adapter.FinaceDetailItemAdapters;
import com.dianrui.agent.bean.FinaceDetailBean;
import com.dianrui.agent.util.JsonUtils;
import com.dianrui.agent.okhttp.OkHttpRequest;
import com.dianrui.agent.util.SharedUtil;
import com.dianrui.agent.util.ToastUtil;
import com.dianrui.agent.util.Url;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FinaceDetailActivity extends BaseActivity {
   private List<FinaceDetailBean> finaceDetailist=new ArrayList<>();

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.show_date)
    TextView showDate;
    @BindView(R.id.day)
    TextView day;
    @BindView(R.id.mRecycleView)
    RecyclerView mRecycleView;

    FinaceDetailItemAdapters adapter;

    @Override
    public int getContentViewId() {
        return R.layout.activity_fiance_detail;
    }

    @Override
    public void initView() {
        title.setText("财务详情");

        getFinceDetail();

        mRecycleView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        adapter=new FinaceDetailItemAdapters(this,finaceDetailist);
        mRecycleView.setAdapter(adapter);
    }


    private void getFinceDetail() {
        String start_time=getIntent().getStringExtra("start_time");
        if(TextUtils.isEmpty(start_time)){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date(System.currentTimeMillis());
            start_time = simpleDateFormat.format(date);
        }
        String end_time=getIntent().getStringExtra("end_time");
        if(TextUtils.isEmpty(end_time)){
            end_time=start_time;
        }
        JsonObject finaceListJson = new JsonObject();
        finaceListJson.addProperty("start_time", start_time);
        finaceListJson.addProperty("end_time", end_time);
        OkHttpRequest.getInstance().postTokenRequests(SharedUtil.getUrl() + Url.FINACE_DETAIL,
                finaceListJson.toString(), new OkHttpRequest.XCallBack() {

                    @Override
                    public void onResponse(String result) {
                        String status= JsonUtils.getJsonStr(result,"status");
                        if (status.equals("1000")) {
                            // 接口返回
                            String data= JsonUtils.getJsonStr(result,"data");
                            String time=JsonUtils.getJsonStr(data,"time");
                            String days=JsonUtils.getJsonStr(data,"days");
                            showDate.setText(time);
                            day.setText("共" + "(" + days + ")" + "天" + "");
                            if (finaceDetailist != null) {
                                finaceDetailist.clear();
                            }
                            List<FinaceDetailBean> list=JsonUtils.jsonStrToList(JsonUtils.getJsonStr(data,"total_list"),new TypeToken<List<FinaceDetailBean>>(){}.getType() );
                            if(list!=null&&!list.isEmpty()){
                                finaceDetailist.addAll(list);
                                adapter.setNewData(finaceDetailist);
                            }

                        }else if ("1001".equals(JsonUtils.getJsonStr(result,"status"))) {
                            ToastUtil.showToast(JsonUtils.getJsonStr(result,"message"));
                        }else if ("1002".equals((JsonUtils.getJsonStr(result,"status")))) {
                            spUtils.clear();
                            ToastUtil.showToast(JsonUtils.getJsonStr(result,"message"));
                            JumpActivitys(LoginActivity.class);
                            finish();
                        }
                    }

                    @Override
                    public void FailedRequest(String result) {

                    }
                });
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
